package com.example.demo.leetcode.classics150;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _79二叉搜索树迭代器
 * @description
 * @date 2023/08/03 17:07
 * <p>
 * ******************************************************
 */
public class _79二叉搜索树迭代器 {

    List<Integer> data;
    Iterator<Integer> iterator;


    public _79二叉搜索树迭代器(TreeNode root) {
        data = new LinkedList<>();
        inOrder(root, data);
        iterator = data.iterator();
    }

    private void inOrder(TreeNode root, List<Integer> data) {
        if (root == null) {
            return;
        }
        inOrder(root.left, data);
        data.add(root.val);
        inOrder(root.right, data);
    }

    public int next() {
        return iterator.next();
    }

    public boolean hasNext() {
        return iterator.hasNext();
    }


   public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
